package com.mju.config;

import java.io.IOException;

import org.json.JSONObject;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import com.mju.utils.RespData;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class AuthFailureHandler implements AuthenticationFailureHandler{

	@Override
	public void onAuthenticationFailure(HttpServletRequest req, HttpServletResponse resp,
			AuthenticationException exception) throws IOException, ServletException {
		resp.setContentType("application/json;charset=utf-8");
		RespData respData = RespData.failed(exception);
		JSONObject json = (JSONObject) JSONObject.wrap(respData);
		resp.getWriter().write(json.toString());
		log.error(json.toString());
		
	}
}
